package com.project.common.exception;

import com.alibaba.fastjson.JSONObject;

public class ResultResponse {

    //返回代码（错误代码）
    private String code;

    //返回信息（错误信息）
    public String Msg;

    //相应结果（返回的数据）
    public Object data;

    public ResultResponse() {
    }

    public ResultResponse(BaseErrorInfoInterface errorInfoInterface) {
        this.code = errorInfoInterface.getResultCode();
        this.Msg = errorInfoInterface.getResultMsg();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return Msg;
    }

    public void setMsg(String msg) {
        Msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    //成功无返回
    public static ResultResponse success() {
        return success(null);
    }

    //成功有返回
    public static ResultResponse success(Object data) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(ExceptionEnum.SUCCESS.getResultCode());
        rb.setMsg(ExceptionEnum.SUCCESS.getResultMsg());
        rb.setData(data);
        return rb;
    }

    //失败
    public static ResultResponse error(BaseErrorInfoInterface errorInfo) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(errorInfo.getResultCode());
        rb.setMsg(errorInfo.getResultMsg());
        rb.setData(null);
        return rb;
    }

    //失败，code和msg
    public static ResultResponse error(String code, String msg) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(code);
        rb.setMsg(msg);
        rb.setData(null);
        return rb;
    }

    public static ResultResponse error(String msg) {
        ResultResponse rb = new ResultResponse();
        rb.setCode("-1");
        rb.setMsg(msg);
        rb.setData(null);
        return rb;
    }

    @Override
    public String toString(){
        return JSONObject.toJSONString(this);
    }

}
